package com.ddwl.gate.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.gate.service.ByteDanceService;
import com.ddwl.schema.bo.gate.byteDance.ByteOrderBo;
import com.ddwl.schema.bo.gate.byteDance.ByteQRCodeBo;
import com.ddwl.schema.bo.gate.byteDance.ByteRefundBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.funds.ByteDanceOrderVo;
import com.ddwl.schema.vo.funds.ByteDanceQueryOrderVo;
import com.ddwl.schema.vo.funds.ByteDanceQueryRefundVo;
import com.ddwl.schema.vo.funds.ByteDanceRefundVo;
import com.ddwl.schema.vo.gate.byteDance.ByteDanceInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author hemiaojun
 * @Date 2021/8/3 16:30
 */
@Api(tags = "字节跳动服务")
@RequestMapping("/byteDance")
@RestController
public class ByteDanceController extends BaseController {

    @Resource
    private ByteDanceService byteDanceService;


    @ApiOperation("获取access_token")
    @GetMapping("/token")
    @Auth(required = false,product = BaseEnum.ProductEnum.BYTE_DANCE)
    public RestVo<String> token(){
        return RestVo.ok(byteDanceService.token());
    }

    @ApiOperation("登录")
    @GetMapping("/login")
    @Auth(required = false,product = BaseEnum.ProductEnum.BYTE_DANCE)
    public RestVo<ByteDanceInfoVo> login(@RequestParam(value = "code",required = false) String code){
        return RestVo.ok(byteDanceService.login(code,getProduct(),getDk()));
    }

    @ApiOperation("获取二维码")
    @PostMapping("/createQRCode")
    @Auth(required = false,product = BaseEnum.ProductEnum.BYTE_DANCE)
    public RestVo<String> createQRCode(@RequestBody ByteQRCodeBo bo){
        return RestVo.ok(Base64.encodeBase64String(byteDanceService.createQRCode(bo)));
    }

    /**
     * 预下单
     * @param bo
     * @return
     */
    @ApiOperation("预下单")
    @PostMapping("/createOrder")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ByteDanceOrderVo> createOrder(@RequestBody ByteOrderBo bo){
        return RestVo.ok(byteDanceService.createOrder(bo));
    }

    /**
     * 订单查询
     * @param bo
     * @return
     */
    @ApiOperation("订单查询")
    @PostMapping("/queryOrder")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ByteDanceQueryOrderVo> queryOrder(@RequestBody ByteOrderBo bo){
        return RestVo.ok(byteDanceService.queryOrder(bo));
    }

    /**
     * 退款
     * @param bo
     * @return
     */
    @ApiOperation("退款")
    @PostMapping("/createRefund")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ByteDanceRefundVo> createRefund(@RequestBody ByteRefundBo bo){
        return RestVo.ok(byteDanceService.createRefund(bo));
    }


    /**
     * 查询退款
     * @param bo
     * @return
     */
    @ApiOperation("查询退款")
    @PostMapping("/queryRefund")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ByteDanceQueryRefundVo> queryRefund(@RequestBody ByteRefundBo bo){
        return RestVo.ok(byteDanceService.queryRefund(bo));
    }


}
